<?php
namespace app\appadmin\controller\v1;
use app\app\Appbase;
use app\appadmin\controller\v1\User;

//
//我的信息
//
class My extends Appbase 
{
	/**
    * 构造函数自动执行
    * @return null
    */
    public function __construct(){
    	if( request()->identity == 1 ){
        	return $this->ReError( '管理员无该操作权限！' );
    	}
		parent::__construct();
    }
	
	/**
	* 我的信息
	* /appadmin/v1/my/index
	* 是否需要登录 
	* @is_need_login true
	*/
	public function index(){
		try {
	        $res = model('shop_staff')->where('id',request()->userId)
	        	->field("id,shop_id,gh,name,phone,img,work_type,sex,mark,status")
	        	->find();
			return $this->ReSucess( $res );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 编辑我的信息
	* /appadmin/v1/my/edit
	* 是否需要登录 
	* @is_need_login true
	*/
	public function edit($name='',$img=''){
		try {
	        $res = model('shop_staff')->where('id',request()->userId)
	        	->field("id,shop_id,gh,name,phone,img,work_type,sex,mark,status")
	        	->find();
	        $res->name = $name ?: $res->name;
	        $res->img = $img ?: $res->img;
	        $res->save();
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 我的绩效
	* /appadmin/v1/my/jx
	* 是否需要登录 
	* @is_need_login true
	*/
	public function jx(){
		$shopId = request()->shopId;
		$identity = request()->identity;
		$uid = request()->userId;
		try {
			
			//本月
			$t1 = "DATE_FORMAT(d.add_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')";
			//上月
			$t2 = "date_format(d.add_time,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')";
			//TODO 
			$isRemind =  getSystem("buess_shop_khls_remind",$shop_id) ?? 30;
			
			$data = [
				'staff_id'=>$uid,
				'shop_id'=>$shopId,
				//服务项目业绩
				'fw'=> $this->getYjModel($uid,$shopId,$t1)->where('b.type','in',"1,2")->sum('b.yj'),
				//普通产品业绩
				'pt'=> $this->getYjModel($uid,$shopId,$t1)->where('b.type','not in',"1,2")->sum('b.yj'),
				//保低业绩
				'bd'=> $identity == 3 ? (model('shop_staff')->where('id',$uid)->value('base_yj') ?: 0) : 0,
				//本月业绩
				'by'=> $by = $this->getYjModel($uid,$shopId,$t1)->sum('b.yj'),
				//上月业绩
				'sy'=> $sy = $this->getYjModel($uid,$shopId,$t2)->sum('b.yj'),
				//业绩增长
				'zz'=> round($by - $sy,2),
				//本月服务次数
				'cs'=> model('shop_yy_order')
					->where('shop_id',$shopId)
					->where('staff_id',$uid)
					->whereTime('add_time','month')
					->where('status',1)
					->count(),
				//项目得分
				'df'=> model('shop_yy_order')
					->where('shop_id',$shopId)
					->where('staff_id',$uid)
					->whereTime('add_time','month')
					->where('status', 1)
					->sum('service_score'),
				//负责客户数
				'kh'=> User::instance(true)->fzkh('','',1),
				//即将流失客户
				'ls' => User::instance(true)->fzkh('','',2),
				//潜在项目
				'qz'=> model('shop_qzproj')->where('add_id',$uid)->count(),
				//待护理项目
				'dhl'=> User::instance(true)->dhlProj(),
				//套餐到期
				'tcdq'=> User::instance(true)->tcdq(),
				//余额不足
				'yebz'=> User::instance(true)->yebz()
			];
			
			return $this->ReSucess($data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	//获取业绩的模型
	private function getYjModel($id,$shopId,$t=''){
		$t = $t ?: "DATE_FORMAT(d.add_time,'%Y%m') = DATE_FORMAT( CURDATE( ) ,'%Y%m' )";
		$identity = request()->identity;
		$model = model('shop_staff')->alias('a')->where('a.id',$id);
		if($identity==2){
			$model = model('shop')->alias('a')->where('a.id',$shopId);
		}
        $model = $model	->leftJoin("(
        		select a.spread_uid,ifnull(b.type,0) type,
        			count(*) as user_num,
        			ifnull(b.yj,0) + ifnull(c.yj,0) + ifnull(g.yj,0)   as yj,
        			ifnull(e.tc,0) + ifnull(f.tc,0) + ifnull(y.tc,0)   as tc
        		from xw_user as a
        		
        		left join (
        			select d.uid,d.type,sum(d.pay_price)  as yj
        			from xw_order d  
        			where d.is_pay = 1 and d.status >=0 and $t
        			group by d.uid,d.type
        		) b on a.id = b.uid 
        		left join (
        			select d.uid,d.type,sum(c.number) as tc
        			from xw_order d  
        			left join xw_user_bill c on c.link_id = d.order_no 
        				and c.category = 'brokerage' 
        				and c.type = 'rake-back' 
        				and c.pm =1
        			where d.is_pay = 1 and d.status >=0 and $t
        			group by d.uid,d.type
        		) e on a.id = e.uid and e.type = b.type
        		
        		left join (
        			select u.spread_uid,d.type,sum(d.pay_price)  as yj
        			from xw_user  u
        			left join xw_order as d on u.id = d.uid and d.is_pay = 1 and d.status >=0 and $t
        			group by u.spread_uid,d.type
        		) c on c.spread_uid = a.id and c.type = b.type
        		left join (
        			select u.spread_uid,d.type, sum(c.number) as tc
        			from xw_user  u
        			left join xw_order as d on u.id = d.uid and d.is_pay = 1 and d.status >=0 and $t
        			left join xw_user_bill c on c.link_id = d.order_no 
        				and c.category = 'brokerage' 
        				and c.type = 'rake-back' 
        				and c.pm =1
        			group by u.spread_uid,d.type
        		) f on f.spread_uid = a.id and f.type = b.type
        		
        		left join (
        			select u.spread_uid,d.type,sum(d.pay_price)  as yj
        			from xw_user  u
        			left join xw_user as k on u.id = k.spread_uid 
        			left join xw_order as d on k.id = d.uid and d.is_pay = 1 and d.status >=0 and $t
        			group by u.spread_uid,d.type
        		) g on c.spread_uid = a.id and c.type = b.type
        		left join (
        			select u.spread_uid,d.type, sum(c.number) as tc
        			from xw_user  u
        			left join xw_user as k on u.id = k.spread_uid 
        			left join xw_order as d on k.id = d.uid and d.is_pay = 1 and d.status >=0 and $t
        			left join xw_user_bill c on c.link_id = d.order_no 
        				and c.category = 'brokerage' 
        				and c.type = 'rake-back' 
        				and c.pm =1
        			group by u.spread_uid,d.type
        		) y on f.spread_uid = a.id and f.type = b.type
        		
        		group by a.spread_uid,ifnull(b.type,0)
        	) b",'b.spread_uid = a.uid');
        return $model;	
	}
	
}



